package logic

import (
	"context"
	"goods_srv/model/mysql"

	"goods_srv/goods"
	"goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品列表

func (l *GoodsListLogic) GoodsList(in *goods.GoodsListReq) (*goods.GoodsListResp, error) {
	g := mysql.Goods{}
	list, err := g.GoodsList(in.Keyword, int(in.Page), int(in.PageSize))
	if err != nil {
		logx.Error("查询列表失败")
		return nil, err
	}
	var goodsList []*goods.GoodsItem
	for _, v := range list {
		goodsList = append(goodsList, &goods.GoodsItem{
			GoodsTitle: v.GoodsTitle,
			GoodsPrice: v.GoodsPrice,
			GoodsStock: v.GoodsStock,
			GoodsCover: v.GoodsCover,
			GoodStatus: int64(v.GoodsStatus),
		})
	}
	return &goods.GoodsListResp{GoodsList: goodsList}, nil
}
